package com.vtc.session10;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.vtc.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LaptopAdapter extends BaseExpandableListAdapter{

	private Context context;
	private List<String> groupList;
	private HashMap<String, List<String>> childList = new HashMap<String,List<String>>();
	List<String> hpModels = new ArrayList<String>();
	
	
	List<String> hclModels =  new ArrayList<String>();
	
	public LaptopAdapter(Context context){
		this.context = context;
		groupList = new ArrayList<String>();
	    groupList.add("HP");
	    groupList.add("Dell");
	    groupList.add("Lenovo");
	    groupList.add("Sony");
	    groupList.add("HCL");
	    groupList.add("Samsung");
	
	    hpModels.add("HP Pavilion G6-2014TX");
		hpModels.add("ProBook HP 4540");
		hpModels.add("HP Envy 4-1025TX");
	    
		hclModels.add("HCL S2101");
		hclModels.add("HCL L2102");
		hclModels.add("HCL V2002");
		
	    childList.put("HP", hpModels);	    
	    childList.put("Dell", hclModels);
	    
	}
	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int posGroup,final int posChild, boolean arg2, View view,
			ViewGroup arg4) {
		RelativeLayout layout=null;
		final String groupKey = groupList.get(posGroup);
		final String childLabel = this.childList.get(groupKey).get(posChild);
		if(view==null){			
		    LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    layout = (RelativeLayout) inflat.inflate(com.vtc.R.layout.laptop_childrenitem, null);
		}
		else{
			layout = (RelativeLayout) view;
		    
		}
		TextView txtLabel = (TextView)layout.findViewById(R.id.laptoplabel);
		ImageView imgView = (ImageView)layout.findViewById(R.id.icondel);
		imgView.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Do you want to remove?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                LaptopAdapter.this.childList.get(groupKey).remove(posChild);
                                notifyDataSetChanged();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
			}
		});
	    txtLabel.setText(childLabel);		    
	    
		return layout;
	}

	@Override
	public int getChildrenCount(int position) {
		String groupKey = groupList.get(position);
		return this.childList.get(groupKey).size();		
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		
		return groupList.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int position, boolean arg1, View view, ViewGroup arg3) {
		// TODO Auto-generated method stub
		LinearLayout layout = null;
		String label = groupList.get(position);
		if(view==null){
			LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    layout = (LinearLayout) inflat.inflate(com.vtc.R.layout.laptop_groupitem, null);		    
		}
		else{
			layout = (LinearLayout) view;		    
		}
		
		TextView txtLabel =(TextView) layout.findViewById(R.id.laptop_grouplabel);
	    txtLabel.setText(label);
		return layout;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

}
